package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.service.OrgAbolishAndMergeService;
import cc.rengu.redp.bizimpl.upmp.dao.*;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service("orgAbolishAndMergeService")
@Slf4j
public class OrgAbolishAndMergeServiceImpl implements OrgAbolishAndMergeService {
    @Autowired
    private UpmpBMchntBaseInfoMapper bMchntBaseInfoDao;
    @Autowired
    private AgentInfoMapper agentInfoDao ;
    @Autowired
    private UpmpBPlatMchntInfoMapper platMchntInfoDao;
    @Autowired
    private ManagerInfoMapper managerInfoDao;
    @Autowired
    private UpmpUserInfoMapper userDao;

    @Override
    public List<Map> findByPage(String instId,String signinstid) {
        List<Map> list = new ArrayList<Map>();
        String[] type = {"商户基本信息", "代理机构信息", "平台商户信息", "拓展经理信息", "内管用户信息"};
        for (int i = 0; i < type.length; i++) {
            Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
            if (type[i].equals("商户基本信息")) {
                List<UpmpBMchntBaseInfo> bMchntBaseInfoList = bMchntBaseInfoDao.getBMchntBaseInfoBySignInstId(signinstid,instId);
                result.put("instId", instId);       //法人行
                result.put("signinstid",signinstid); //原机构
                result.put("paramKey", type[i]);    //变更数量
                if (CollectionUtils.isEmpty(bMchntBaseInfoList)) {
                    result.put("paramValue", "0");
                } else {
                    result.put("paramValue", bMchntBaseInfoList.size());
                }
            } else if (type[i].equals("代理机构信息")) {
                result.put("instId", instId);       //法人行
                result.put("signinstid",signinstid); //原机构
                result.put("paramKey", type[i]);    //变更数量
                List<AgentInfo> AgentInfoList = agentInfoDao.getAgentInfoBySignInstId(signinstid,instId);
                if (CollectionUtils.isEmpty(AgentInfoList)) {
                    result.put("paramValue", "0");
                } else {
                    result.put("paramValue", AgentInfoList.size());
                }
            } else if (type[i].equals("平台商户信息")) {
                result.put("instId", instId);       //法人行
                result.put("signinstid",signinstid); //原机构
                result.put("paramKey", type[i]);    //变更数量
                List<UpmpBPlatMchntInfo> platMchntInfoList = platMchntInfoDao.getPlatMchntInfoBySignInstId(signinstid,instId);
                if (CollectionUtils.isEmpty(platMchntInfoList)) {
                    result.put("paramValue", "0");
                } else {
                    result.put("paramValue", platMchntInfoList.size());
                }

            } else if (type[i].equals("拓展经理信息")) {
                result.put("instId", instId);       //法人行
                result.put("signinstid",signinstid); //原机构
                result.put("paramKey", type[i]);    //变更数量
                List<ManagerInfo> ManagerInfoList = managerInfoDao.getManagerInfoBySignInstId(signinstid,instId);
                if (CollectionUtils.isEmpty(ManagerInfoList)) {
                    result.put("paramValue", "0");
                } else {
                    result.put("paramValue", ManagerInfoList.size());
                }
            } else if (type[i].equals("内管用户信息")) {
                result.put("instId", instId);       //法人行
                result.put("signinstid",signinstid); //原机构
                result.put("paramKey", type[i]);    //变更数量
                List<UpmpUserInfo> UserList = userDao.getUserBySignInstId(signinstid,instId);
                if (CollectionUtils.isEmpty(UserList)) {
                    result.put("paramValue", "0");
                } else {
                    result.put("paramValue", UserList.size());
                }
            }
            list.add(result);
        }
        return list;
    }

    @Override
    public OperationResult saveData(HttpServletRequest request) {
        String instId = request.getParameter("instId");
        String signinstid = request.getParameter("signinstid");
        String paramKey = request.getParameter("paramKey");
        String newsigninstid = request.getParameter("newsigninstid");
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");

        if (paramKey.equals("商户基本信息")) {
            List<UpmpBMchntBaseInfo> BMchntBaseInfoList = bMchntBaseInfoDao.getBMchntBaseInfoBySignInstId(signinstid, instId);
            for (UpmpBMchntBaseInfo bmchntbaseinfo : BMchntBaseInfoList) {
                    bmchntbaseinfo.setLastUpdateTime(lastUpddt);
                    bmchntbaseinfo.setSignInstId(newsigninstid);
                    bMchntBaseInfoDao.save(bmchntbaseinfo);
            }
        } else if (paramKey.equals("代理机构信息")) {
            List<AgentInfo> AgentInfoList = agentInfoDao.getAgentInfoBySignInstId(signinstid,instId);
            for (AgentInfo agentinfo : AgentInfoList) {
                agentinfo.setLastUpdateTime(lastUpddt);
                agentinfo.setManageInstId(newsigninstid);
                agentInfoDao.save(agentinfo);
            }
        }else if (paramKey.equals("平台商户信息")) {
            List<UpmpBPlatMchntInfo> PlatMchntInfoList = platMchntInfoDao.getPlatMchntInfoBySignInstId(signinstid,instId);
            for (UpmpBPlatMchntInfo platmchntinfo : PlatMchntInfoList) {
                platmchntinfo.setLastUpdateTime(lastUpddt);
                platmchntinfo.setManageInstId(newsigninstid);
                platMchntInfoDao.save(platmchntinfo);
            }
        }else if (paramKey.equals("拓展经理信息")) {
            List<ManagerInfo> ManagerInfoList = managerInfoDao.getManagerInfoBySignInstId(signinstid,instId);
            for (ManagerInfo managerinfo : ManagerInfoList) {
                managerinfo.setLastUpdateTime(lastUpddt);
                managerinfo.setSignInstId(newsigninstid);
                managerInfoDao.save(managerinfo);
            }
        }else if (paramKey.equals("内管用户信息")) {
            List<UpmpUserInfo> UserList = userDao.getUserBySignInstId(signinstid,instId);
            for (UpmpUserInfo user : UserList) {
                user.setOrgCode(newsigninstid);
                userDao.updateUser(newsigninstid,user.getId());
            }
        }
        return OperationResult.buildSuccessResult("机构撤并完成");
    }
}
